﻿@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>layout 后台大布局 - Layui</title>
    <!--SEO优化部分 & 全局样式部分-->
    @Html.Partial("_Title")
    <script src="/Theme/layui/layui.js"></script>
    <script src="/Theme/layer/layer.js"></script>
    <link href="~/Theme/MyCSS/EditPopup.css" rel="stylesheet" />
</head>
<body class="">

    <div style="overflow:hidden; display:none" id="EditTitlecontentid">
        <!-- 内容展示区域 -->
        <div style="padding-left: 10px; padding-right: 10px;">
            <table lay-filter="parse-table-demo2">
                <thead>
                    <tr>
                        <th lay-data="{field:'subject', width:350}">产品标题</th>
                        <th lay-data="{field:'amount', width:200}">原可售数量</th>
                        <th lay-data="{field:'UpdateSubject', width:200}">修改后可售数量</th>
                    </tr>
                </thead>
                <tbody id="EditTitleProlist"></tbody>
            </table>
            <div id="demo1"></div>
        </div>
        <!-- 操作区 -->
        <div class="title-rules">
            <div>
                @foreach (var r in ViewBag.Category_AttributeInfo)
                {
                    if (r.required == "True" && r.aspect == "0;")
                    {
                        <div style="color:red;">
                            @r.name
                        </div>
                    }
                    else
                    {
                        <div>
                            @r.name
                        </div>
                    }

                }
            </div>
        </div>
        <!-- 保存提交区 -->
        <div class="Update-but">
            <button class="layui-btn layui-btn-warm layui-btn-small" onclick="previewUpadate()">预览修改</button>
            <button class="layui-btn layui-btn-small layui-btn-normal" onclick="SubmitUpdate()">保存修改</button>
        </div>
    </div>

    <script>
        //页面初始化
        $(function () {
            //初次加载数据
            RequestProList(1,false);
        })

        

        //请求数据，解析数据，异步加载要显示的产品数据列表
        function RequestProList(PageIndex, isone) {

            //获取父窗体参数
            var parameter = parent.ToMDIChildParameter;
            parameter["PageIndex"] = PageIndex;//请求页

            //如果用户要编辑是当前选中的产品。
            if (parameter["OperationObjType"] == "CurrentProsUpdate")
            {
                var products = parent.newallproducts;
                var lengthse = products.length;

                //分页
                products = products.slice((PageIndex - 1) * 10, (10 + ((PageIndex - 1) * 10)));
                //渲染数据
                RenderingData(products, PageIndex, lengthse);
                return;
            }


            if (isone)
            {
                //提示弹层
                layer.msg('正在加载数据...', { icon: 16, shade: [0.5, '＃f5f5f5'], scrollbar: false, offset: '100px', time: 50000 });
            }

            $.ajax({
                url: "/api/EditSalesVolumesAPI/Products",
                type: "Post",
                dataType: "json",
                data:parameter,
                success: function (data) {

                    //解析产品集合
                    var objs = JSON.parse(data);
                    var Currentcount = objs["Currentcount"];
                    var products = objs["products"];

                    //渲染数据
                    RenderingData(products,PageIndex,Currentcount);

                    //关闭弹层提示
                    layer.closeAll();
                }
            })
        }

        //渲染数据
        var RenderingData = function (products,PageIndex,Currentcount)
        {
            var htmls = "";
            for (var i = 0; i < products.length; i++) {

                htmls += "<tr>"
                htmls += "<td>" + products[i]["subject"] + "</td>";
                htmls += "<td>" + products[i]["amount"] + "</td>";
                htmls += "<td><div></div></td>";
                htmls += "</tr>"
            }

            //插入htnl代码
            $("#EditTitleProlist").html(htmls);

            //渲染表格
            layui.use('table', function () {
                var table = layui.table;
                table.init('parse-table-demo2', { //转化静态表格
                    height: '250'
                });

                //避免屏幕闪动，在Layui脚本渲染后显示表格区域
                $("#EditTitlecontentid").show();
            });

            if (isEdit) {
                //重新刷新预览
                previewUpadate();
            }

            //如果请求的是第一页，重新绑定分页
            if (PageIndex == 1) {
                //分页部分脚本。
                ProductsPaging(10, Currentcount);
            }

        }

        //分页部分脚本。
        var ProductsPaging = function (nums, Currentcount) {
            //分页
            layui.use(['laypage', 'layer'], function () {
                var laypage = layui.laypage;
                var layer = layui.layer;

                laypage.render({
                    skin: "",
                    elem: 'demo1',
                    count: Currentcount,
                    limit: nums,
                    layout: ['page', 'count'],
                    curr: function (obj) {
                         isone = false;
                    }(),
                    jump: function (obj) {

                        if (isone) {
                            //请求数据。
                            RequestProList(obj.curr,true);
                        }
                        else {
                            isone = true;
                        }
                    }
                });
            })
        }

        //标示，是否已经预览过。
        var isEdit = false;
        //预览修改
        var previewUpadate = function () {
            isEdit = true;
            //操作方式
            var operationMode = $("input[name='modifyTitle']:checked").val();
            //选择的参数
            var currentVal = $("input[name='modifyTitle']:checked").parent().nextAll(1).val()

            if (currentVal == "" || currentVal == 0) {
                layer.alert("操作值不能为空或0"); return false;
            }


            //循环产品标题列表
            $("table.layui-table tbody tr").each(function () {
                var Volume = $(this).find("td").eq(1).find("div").text();
                var newVolume = replaceSalesVolume(operationMode, currentVal, Volume);
                $(this).find("td").eq(2).find("div").html(newVolume);
            })

        }

        //计算可售数量
        var replaceSalesVolume = function (operationMode, currentVal, Volume) {

            //第一操作组
            if (operationMode == 1) {
                return parseInt(currentVal) + parseInt(Volume);
            }
                //第二操作组
            else if (operationMode == 2) {
                if (parseInt(Volume) > parseInt(currentVal)) {
                    return parseInt(Volume) - parseInt(currentVal);
                }
                else { return 0; }
            }
                //第三操作组
            else if (operationMode == 3) {
                return currentVal;
            }
            else { return "<span class='Highlight'>未找到匹配项</span>" }
        }

    </script>
    <script>


    </script>


</body>
</html>

